const http = require('http')
const mysql = require('mysql2')
const server = http.createServer(function (req, res) {
    //当前端请求/user路径时，将数据库中的user表中的数据读取并返回给前端
    if (req.url === '/user') {
        const connection = mysql.createConnection({//创建一个连接
            host: 'localhost',
            user: 'root',
            password: '123456',
            database: 'demo'//连接的库名
        })
        connection.query('select * from user', function (err, data) {
            res.end(JSON.stringify(data))//将data数组转换为json字符串
            //end内只能放原始类型
        })
    }
})
server.listen(3000, () => console.log('server is running'))

